﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;

namespace VoiceTestPlatform
{
    internal class DefaultWindowsService : IWindowsService
    {
      

        static DefaultWindowsService()
        {
            maps = new Dictionary<Type, Type>();
        }

        public static void Regsiter(Type viewModelType, Type WindowType)
        {
            maps.Add(viewModelType, WindowType);
        }

        static private Dictionary<Type, Type> maps;

        public void ShowWindowByViewModel(ViewModelBase vm)
        {

            var wt = maps[vm.GetType()];
            object obj = Activator.CreateInstance(wt);
            var win = obj as Window;
            if (win != null)
            {
                vm.RequestCloseHandler += (s, e) => {
                    win.Close();
                };
                win.Owner = App.Current.MainWindow; 
                win.DataContext = vm;
                win.Show();
            }
            else
            {
                throw new Exception("窗体创建错误");
            }
        }


        public void ShowMessageBox(string msg)
        {
            MessageBox.Show(msg);
        }


        public Stream SaveFileDialog(string filename, string filter = "", string defaultExt = "")
        {
            var dialog = new SaveFileDialog
            {
                //DefaultExt = extension,
                Filter = filter,//string.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
                FilterIndex = 1,
                FileName = filename,
            };
            if (dialog.ShowDialog() != true) return null;
            return dialog.OpenFile();
        }


        public string[] OpenFileDialog(string filter, string initialDirectory = "", bool Multiselect = false)
        {
            Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
            op.InitialDirectory = initialDirectory;
            op.RestoreDirectory = false;
            op.Multiselect = Multiselect;
            op.Filter = filter;
            if (op.ShowDialog() == true)
            {
                return op.FileNames;
               
            }
            return null;
        }
    }
}
